<?php

class projects_Controller extends Controller {
    
    public function _default($params) {
        $view = new projects_View(new Module((int)$params['module']));
        $view->createView('menu',$params);
        return $view;
    }
    
    public function add($params) {
        $view = new projects_View(new Module((int)$params['module']));
        if(isset($_POST['project'])) {
            $project = new Project();
            $project->setData($_POST['project']);
            if(!($pid = $project->create())) {
                $view->createView('addError',$params);
            } else {
                $view->createView('addSuccess',array_merge($params,array("pid"=>$pid)));
            }
        } else {
            $view->createView('add',$params);
        }
        return $view;        
    }
    
    public function delete($params) {
        $view = new projects_View(new Module((int)$params['module']));
        if(isset($_POST['pid'])) {
            $project = new Project((int)$_POST['pid']);
            if(!$project->delete()) {
                $view->createView('deleteError',$params);
            } else {
                $view->createView('deleteSuccess',array_merge($params,array("project"=>$project)));
            }
        } else {
            $view->createView('delete',$params);
        }
        return $view;        
    }
    
    public function manage($params) {
        $view = new projects_View(new Module((int)$params['module']));
        $view->createView('manage',$params);
        return $view;        
    }
    
    public function manageProject($params) {
        $view = new projects_View(new Module((int)$params['module']));
        $view->createView('manageProject',$params);
        return $view;
    }
    
    public function addTask($params) {
        $view = new projects_View(new Module((int)$params['module']));
        if(isset($_POST['task'])) {
            $project = new Project((int)$params['pid']);
            $task = new ProjectTask();
            $task->setNewTaskData($_POST['task']);
            if($task->addTask($project)) {
                $view->createView('addTaskSuccess',array_merge($params,array('task'=>$task)));
            } else {
                $view->createView('addTaskError',$params);
            }
        } else {
            $view->createView('addTask',$params);
        }
        return $view;
    }
    
    public  function addUser($params) {
        $view = new projects_View(new Module((int)$params['module']));
        if(isset($_POST['uid'])) {
            $task = new ProjectTask($params['ptid']);
            if($task->assignUser(new AnyUser((int)$_POST['uid']))) {
                unset($params['ptid']);
                $view->createView('addUserSuccess', $params);
            } else {
                $view->createView('addUserError',$params);
            }
        } else {
            $view->createView('addUser',$params);
        }
        return $view;
    }
    
    public function editTask($params) {
        $view = new projects_View(new Module((int)$params['module']));
        if(isset($_POST['task'])) {
            $task = new ProjectTask((int)$params['ptid']);
            $task->setNewTaskData($_POST['task']);
            if($task->updateTask()) {
                $view->createView('updateTaskSuccess',array_merge($params,array('task'=>$task)));
            } else {
                $view->createView('updateTaskError',$params);
            }
        } else {
            $view->createView('updateTask',$params);
        }
        return $view;
    }
}

?>
